//
//  Extension.swift
//  xiaofenshu
//
//  Created by zhu jinlong on 2021/5/19.
//

import UIKit

extension Bundle {
    var appName:String {
        if let appName = localizedInfoDictionary?["CFBundleDisplayName"] {
            return appName as! String
        }else {
            return infoDictionary?["CFBundleDisplayName"] as! String
        }
    }
}

extension UIView {
    @IBInspectable
    var radius:CGFloat {
        get{
            layer.cornerRadius
        }
        set{
            layer.cornerRadius = newValue
        }
    }
    
    @IBInspectable
    var bounds:Bool {
        set{
            layer.masksToBounds = newValue
        }
        get{
            layer.masksToBounds
        }
    }
    
    @IBInspectable
    var borderColor:UIColor {
        set{
            layer.borderColor = newValue.cgColor
        }
        get{
            UIColor.init(cgColor: layer.borderColor ?? UIColor.clear as! CGColor)
        }
    }
    
    @IBInspectable
    var borderWidth:CGFloat {
        get{
            layer.borderWidth
        }
        set{
            layer.borderWidth = newValue
        }
    }
}

extension UIViewController {
    // MARK: 菊花框
    func showLoadingHud(){
        MBProgressHUD.showAdded(to: self.view, animated: true)
    }
    // MARK: 隐藏菊花框
    func hideLoadingHud(){
        MBProgressHUD.hide(for: view, animated: true)
    }
    // MARK: 提示toast框
    func showToast(title: String, subTitle:String? = nil){
        let hud = MBProgressHUD.showAdded(to: self.view, animated: true)
        hud.mode = .text
        hud.label.text =  title
        if let sub = subTitle {
            hud.detailsLabel.text = sub
        }
        hud.hide(animated: true, afterDelay: 1.5)
    }
    
    // MARK: 点击空白处隐藏键盘
    func hideKeyboardWhenTappedAround(){
        let tap = UITapGestureRecognizer(target: self, action: #selector(dissmissKeyboard))
        tap.cancelsTouchesInView = false
        view.addGestureRecognizer(tap)
    }
    
    @objc private func dissmissKeyboard(){
        view.endEditing(true)
    }
}
